package com.hm.cms.service;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.hm.cms.auto.su.BaseDomain;
import com.hm.cms.def.ExtLlinkEnum;
import com.hm.cms.domain.ExtLinkDomain;
import com.hm.cms.domain.QExtLinkDomain;
import com.hm.cms.repository.ExtLinkRepository;
import com.hm.cms.service.ExtLinkService;
import com.hm.cms.su.BeanUtil;
import com.hm.cms.vo.HcmsExtLinkVo;
import com.hm.common.def.DataStatusDef;
import com.hm.common.exception.ErrorCode;
import com.hm.common.exception.ServiceException;
import com.hm.common.util.CommonUtil;
import com.hm.common.util.DateUtil;

/**
 * @author shishun.wang
 * @date 2017年10月22日 下午12:50:07
 * @version 1.0
 * @describe
 */
@Service
public class ExtLinkServiceImpl implements ExtLinkService {

	@Autowired
	private ExtLinkRepository extLinkRepository;

	@Override
	public void addExtLink(HcmsExtLinkVo extLinkVo, String operationUserId) {
		if (CommonUtil.isAnyEmpty(extLinkVo.getExtLink(), extLinkVo.getLinkName(), extLinkVo.getLinkUri(), extLinkVo.getLinkOrder())) {
			throw ServiceException.warning(ErrorCode.REQUIRED_PARAMETERS_MISSING);
		}

		ExtLinkDomain domain = new ExtLinkDomain();
		BeanUtil.copyProperties(extLinkVo, domain);
		BaseDomain.initBaseInfo(domain);
		domain.setCreateUser(operationUserId);
		extLinkRepository.save(domain);
	}

	@Override
	public List<HcmsExtLinkVo> loadExtLinks(ExtLlinkEnum extLink) {
		if (CommonUtil.isEmpty(extLink)) {
			throw ServiceException.warning(ErrorCode.REQUIRED_PARAMETERS_MISSING);
		}

		List<HcmsExtLinkVo> vos = new ArrayList<HcmsExtLinkVo>();

		QExtLinkDomain qExtLinkDomain = QExtLinkDomain.extLinkDomain;
		extLinkRepository.findAll(qExtLinkDomain.status.eq(DataStatusDef.ENABLE).and(qExtLinkDomain.extLink.eq(extLink))).forEach(link -> {
			HcmsExtLinkVo vo = new HcmsExtLinkVo();
			BeanUtil.copyProperties(link, vo);
			vo.setCreateTime(DateUtil.data2long(link.getCreateTime()));
			vos.add(vo);
		});

		return vos;
	}
}
